﻿using Steamworks;
using StockHelper.Scripts.Comm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StockHelper.Scripts
{
    public class SteamManager
    {
        public static bool Init()
        {
            string sign = Sign();
            if (File.Take("sign.txt") == sign)
            {
                return true;
            };

            if (!SteamAPI.Init())
            {
                MessageBox.Show("steam未启动");
                return false;
            }
            try
            {
                if (SteamAPI.RestartAppIfNecessary(new AppId_t(1452680)))
                {
                    return false;
                }
            }
            catch
            {
                MessageBox.Show("请从steam启动");
                return false;
            }

            Console.WriteLine(SteamAPI.GetHSteamUser().m_HSteamUser);

            File.Save("sign.txt", sign);
            SteamAPI.Shutdown();
            return true;
        }


        private static string Sign() {
            HardwareInfo info = new HardwareInfo();
            string cpu = info.GetCpuID();
            string mac = info.GetMacAddress();
            string disk = info.GetHardDiskID();
            string sign = MD5("Q" + cpu +"h"+ 5 + disk +"DD"+ MD5(mac) + "1");
            return MD5(sign);
        }
        

        /// <summary>
        /// MD5计算
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string MD5(string value)
        {
            byte[] result = Encoding.UTF8.GetBytes(value);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "");
        }

        


    }
}
